package com.lijianlove.keeplearning.strategy;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author lijian
 * @date 2019/4/28 上午11:56
 */
@Component
public class UserStrategy {

    public void saveToDb2(Random random,JdbcTemplate jdbcTemplate) {
        DynamicDataSourceContextHolder.setDataSourceLookupKey("db_2");
        int id2 = random.nextInt();
        if (id2 < 0) {
            id2 = 0 - id2;
        }
        jdbcTemplate.execute("INSERT into user VALUES  (" + id2 + "," + id2 % 100 + ",\" this is db2 \",now())");
    }

    public Random saveToDb1(JdbcTemplate jdbcTemplate) {
        DynamicDataSourceContextHolder.setDataSourceLookupKey("db_1");
        Random random = new Random();
        int id = random.nextInt();
        if (id < 0) {
            id = 0 - id;
        }
        jdbcTemplate.execute("INSERT into user VALUES  (" + id + "," + id % 100 + ",\" this is db1 \",now())");
        return random;
    }
}
